package com.eqdd.yiqidian.ui.discover.makerspace

import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows
import com.eqdd.yiqidian.bean.rebuild.LearnLectureListBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.ui.yiqixue.add.AddLectureActivity
import com.eqdd.yiqidian.ui.yiqixue.info.LectureInfoActivity
import com.example.library.AutoFlowLayout
import com.example.library.FlowAdapter
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import kotlinx.android.synthetic.main.layout_flow_tag.view.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast
import java.util.*

/**
 * Created by jhj on 18-8-7.
 */
class MakerCourseListActivity : BaseActivity() {
    var pageNum = 0
    private var type: Int = 0

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "课程")
        type = intent.getIntExtra(Config.TYPE, 0)
        if (type == 1) {
            initTopRightText("新增") {
                startActivityForResult<AddLectureActivity>(1000)
            }
        }
    }

    private var mvcHelper: MVCCoolHelper<List<LearnLectureListBean>>? = null

    override fun initData() {
        val guid = intent.getStringExtra(Config.GUID)
        val list = arrayListOf<LearnLectureListBean>()
        mvcHelper = MVCCoolHelper<List<LearnLectureListBean>>(coolRefreshView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<LearnLectureListBean>(R.layout.list_item_180, SlimInjector<LearnLectureListBean> { data, injector ->
                    injector
                            ?.with<TextView>(R.id.tv_one) {
                                val str = if (type == 1) {
                                    String.format(Locale.CHINA, "%s(%s-%s天)", data?.courseTheme, data?.lectureName, data?.courseTimes)
                                } else {
                                    String.format(Locale.CHINA, "%s(%s天)", data?.courseTheme, data?.courseTimes)
                                }
                                val span = SpannableString(str)
                                span.setSpan(RelativeSizeSpan(0.8f), data?.courseTheme?.length
                                        ?: 0, str.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
                                span.setSpan(ForegroundColorSpan(ContextCompat.getColor(this, R.color.hint_color)), data?.courseTheme?.length
                                        ?: 0, str.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
                                it.text = span
                            }
                            ?.with<AutoFlowLayout<String>>(R.id.autoFlowLayout) {
                                val type = data?.courseType?.split(",")
                                it.setAdapter(object : FlowAdapter<String>(type) {
                                    override fun getView(p0: Int): View {
                                        val view = LayoutInflater.from(this@MakerCourseListActivity).inflate(R.layout.layout_flow_tag, null)
                                        view.tv_tag.text = type?.get(p0)
                                        return view;
                                    }
                                })
                                it.setOnItemClickListener { i, view ->

                                }
                            }
                            ?.text(R.id.tv_three, data?.createTime);


                })
                .attachTo(recyclerView)
                .updateData(list as List<LearnLectureListBean>?) as SlimAdapterEx<LearnLectureListBean>?
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    startActivity<LectureInfoActivity>(Config.ID to data?.id, Config.TYPE to 1)
                }

        val dataSource = ModelRx2DataSource<LearnLectureListBean>(ModelRx2DataSource.OnLoadSource<LearnLectureListBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResultRows<LearnLectureListBean>>(HttpConfig.BASE_URL + HttpConfig.LEARN_LECTURE_LIST_MINE)
                    .params("userGuid", guid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResultRows<LearnLectureListBean>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(object : FlowableBody<HttpResultRows<LearnLectureListBean>>() {})
                    .flatMap({ listResultBean ->
                        pageNum = listResultBean.getItems().getPage();
                        if (listResultBean.status == 200) {
                            Flowable.just(
                                    if (listResultBean.items == null)
                                        arrayListOf<LearnLectureListBean>()
                                    else
                                        listResultBean.getItems().getRows());
                        } else {
                            toast(listResultBean.msg);
                            Flowable.just(arrayListOf())
                        }
                    });

        }, 10)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.refresh()

    }

    override fun setView() {
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Config.SUCCESS && requestCode == 1000) {
            mvcHelper?.refresh()
        }
    }
}